package service

import (
	"crypto/sha1"
	"fmt"
	"git.tmuyu.com.cn/ginOne/models"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	"io"
	"sort"
	"strings"
)

const (
	token = "babamagic"
)

func Getheader(c *gin.Context) string {
	token := c.GetHeader("x-ai-token")
	if token != "" {
		logrus.Warnf("c.GetHeader(x-auth-token)")
		return token
	}
	return ""
}

func MakeSignature(timestamp, nonce string) string {
	sl := []string{token, timestamp, nonce}
	sort.Strings(sl)
	s := sha1.New()
	io.WriteString(s, strings.Join(sl, ""))
	return fmt.Sprintf("%x", s.Sum(nil))
}

func ValidateUrl(Wxurl models.WxUrl) bool {
	signatureGen := MakeSignature(Wxurl.Timestamp, Wxurl.Nonce)
	if signatureGen != Wxurl.Signature {
		return false
	}
	return true
}
